package com.xuecheng.content.controller;

import com.xuecheng.api.content.CourseBaseApi;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseModel;
import com.xuecheng.api.content.model.vo.CourseBaseVo;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.content.common.util.UAASecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.CoursePubService;
import org.springframework.web.bind.annotation.*;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * <p>
 * 课程基本信息 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
public class CourseBaseController implements CourseBaseApi {

    @Autowired
    private CourseBaseService courseBaseService;


    @Autowired
    private CoursePubService coursePubService;

    /**
     * 分页查询课程基本信息
     *
     * @param params
     * @param model
     * @return
     */
    @Override
    @PostMapping("course/list")
    public PageVO queryCourseList(PageRequestParams params, @RequestBody QueryCourseModel model) {

        Long companyId = UAASecurityUtil.getCompanyId();

        PageVO pageVO = courseBaseService.queryCourseList(params, model, companyId);
        return pageVO;
    }

    /**
     * 课程基本信息修改
     *
     * @param courseBaseVo
     * @return
     */
    @Override
    @PutMapping("course")
    public CourseBaseDTO modifyCourseBase(@RequestBody CourseBaseVo courseBaseVo) {
        Long companyId = UAASecurityUtil.getCompanyId();
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVo);
        courseBaseDTO.setCompanyId(companyId);
        CourseBaseDTO result = courseBaseService.modifyCourseBase(courseBaseDTO);
        return result;
    }

    /**
     * 根据课程id查询课程基础新信息
     *
     * @param courseId
     * @return
     */
    @Override
    @GetMapping("course/{courseId}")
    public CourseBaseDTO getCourseBaseById(@PathVariable Long courseId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        CourseBaseDTO courseBaseDTO = courseBaseService.getCourseBaseById(courseId, companyId);
        return courseBaseDTO;
    }

    /**
     * 教学机构提交课程
     *
     * @param courseBaseId
     */
    @GetMapping("course/commit/{courseBaseId}")
    public void commitCourseBase(@PathVariable Long courseBaseId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        courseBaseService.commitCourseBase(courseBaseId, companyId);
    }

    @Override
    @PostMapping("course")
    public CourseBaseDTO createCourseBase(@RequestBody CourseBaseVo courseBaseVo) {

        Long companyId = UAASecurityUtil.getCompanyId();
        CourseBaseDTO courseBaseDTO = CourseBaseConvert.INSTANCE.vo2dto(courseBaseVo);
        courseBaseDTO.setCompanyId(companyId);

        CourseBaseDTO baseResult = courseBaseService.createCourseBase(courseBaseDTO);

        return baseResult;
    }


    public CourseBaseDTO removeCourseBaseById(@PathVariable Long courseId) {

        Long companyId = UAASecurityUtil.getCompanyId();

        CourseBaseDTO dto = courseBaseService.getCourseBaseById(courseId, companyId);

        return dto;

    }

    @DeleteMapping("course/{courseBaseId}")
    public void removeCoursebase(@PathVariable Long courseBaseId) {
        Long companyId = UAASecurityUtil.getCompanyId();

        courseBaseService.removeCourseBase(courseBaseId, companyId);
    }

    /**
     * 课程预览
     * @param courseId
     * @param companyId
     * @return
     */
    @GetMapping("course/preview/{courseId}/{companyId}")
    public Object preview(@PathVariable Long courseId, @PathVariable Long companyId) {
        // 构建页面模板和数据模型生成html页面

        // service层可以获得数据模型DataMap
        Map<String, Object> dataMap = coursePubService.preview(courseId, companyId);

        // controller将数据模型和页面模板结合起来生成html
        ModelAndView modelAndView = new ModelAndView("learing_article",dataMap);

        return modelAndView;
    }


    @PostMapping("course_pub/publish/{courseBaseId}")
    public void publish(@PathVariable Long courseBaseId) {

    }
}
